package com.blinddate.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.blinddate.context.BaseContext;
import com.blinddate.pojo.entity.AdminKey;
import com.blinddate.pojo.entity.User;
import com.blinddate.pojo.result.Result;
import com.blinddate.pojo.vo.UserLoginVO;
import com.blinddate.properties.JwtProperties;
import com.blinddate.service.AdminKeyService;
import com.blinddate.service.UserService;
import com.blinddate.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/user/key")
@Slf4j
@Api(tags = "关于密钥相关的接口")
public class AdminKeyController {
    @Autowired
    private UserService userService;

    @Autowired
    private AdminKeyService adminKeyService;

    @Autowired
    private JwtProperties jwtProperties;

    @PostMapping("creat_Key")
    @ApiOperation("密钥创建")
    public Result<AdminKey> creatKey(){
        log.info("密钥创建");
        //判断用户权限  必须有超级管理员来创建
        Boolean decide = userService.superAdmin();
        if (!decide){
            return Result.error("用户权限不够");
        }

        String loginKey = UUID.randomUUID().toString();
        AdminKey adminKey = new AdminKey();
        adminKey.setLoginKey(loginKey);
        boolean save = adminKeyService.save(adminKey);
        if (save){
            return Result.success(adminKey);
        }
        return Result.error("生成密钥失败");
    }

    @GetMapping("get_all_key")
    @ApiOperation("得到所有密钥")
    public Result<List<AdminKey>> getAllKey(){
        log.info("得到所有密钥");
        //判断用户权限
        Boolean decide = userService.superAdmin();
        if (!decide){
            return Result.error("用户权限不够");
        }

        List<AdminKey> list = adminKeyService.list();
        if (list==null){
            return Result.error("没有密钥");
        }
        return Result.success(list);
    }

    @DeleteMapping("delete_key")
    @ApiOperation("删除密钥")
    public Result<String> deleteKey(@RequestParam String adminKey){
        log.info("删除密钥,key:{}",adminKey);
        //判断用户权限
        Boolean decide = userService.superAdmin();
        if (!decide){
            return Result.error("用户权限不够");
        }

        LambdaQueryWrapper<AdminKey> keyLambdaQueryWrapper = new LambdaQueryWrapper<>();
        keyLambdaQueryWrapper.eq(AdminKey::getLoginKey,adminKey);
        AdminKey adminKey1 = adminKeyService.getOne(keyLambdaQueryWrapper);
        if (adminKey1==null){
            return Result.error("没有该密钥删除失败");
        }
        boolean remove = adminKeyService.remove(keyLambdaQueryWrapper);
        if (remove){
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

    @PostMapping("admin_create")
    @ApiOperation("通过密钥创建管理员")
    public Result<UserLoginVO> adminCreate(@RequestBody AdminKey adminKey){
        Long userId = BaseContext.getCurrentId();
        //获取用户
        User user = userService.getById(userId);
        //先查看用户是不是管理员
        if (user.getIdentity()==1||user.getIdentity()==2){
            return Result.error("该用户为管理员权限，请不用重复使用密钥");
        }


        log.info("通过密钥创建管理员,key:{}",adminKey);
        LambdaQueryWrapper<AdminKey> keyLambdaQueryWrapper = new LambdaQueryWrapper<>();
        keyLambdaQueryWrapper.eq(AdminKey::getLoginKey,adminKey.getLoginKey());
        AdminKey lognKey = adminKeyService.getOne(keyLambdaQueryWrapper);
        if (lognKey==null){
            return Result.error("没有这个密钥请联系相关人员");
        }

        user.setIdentity(1);
        boolean remove = adminKeyService.remove(keyLambdaQueryWrapper);
        if (!remove){
            return Result.error("请重试");
        }
        //更新用户数据
        boolean updateUser = userService.updateById(user);
        if (!updateUser){
            return Result.error("权限更新失败");
        }
        //将用户信息进行保存
        UserLoginVO userLoginVO = new UserLoginVO();
        BeanUtils.copyProperties(user,userLoginVO);
        log.info("{}",user);
//        //为微信用户生成jwt令牌
        Map<String,Object> cliams=new HashMap<>();
        cliams.put("userid",user.getId());
        String token= JwtUtil.createJWT(jwtProperties.getUserSecretKey(),jwtProperties.getUserTtl(),cliams);
        userLoginVO.setToken(token);
        return Result.success(userLoginVO);

    }


}
